﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using IndignadoFramework.BackOffice.Models;
using IndignadoFramework.BackBone.Model;
using IndignadoFramework.BackBone.InternalServices;
using IndignadoFramework.BackBone.InternalServices.Implementacion;

namespace IndignadoFramework.BackOffice.Controllers
{
    
    public class AccountController : Controller
    {
        private UsuarioAdminServices _usuariosI;
        
        public AccountController()
            : this(new UsuarioAdminServicesImpl())
        {
        }

        public AccountController(UsuarioAdminServices user)
        {
            _usuariosI = user;
            
        }

        // GET: /Account/LogOn
        [HttpGet]
        public ActionResult LogOn()
        {
            
            return View();
        }
        
        [HttpPost]
        public ActionResult LogOn(LogOnModel model)
        {
            if (ModelState.IsValid)
            {
                
                UsuarioAdminModel u = new UsuarioAdminModel();
                u.UsuarioAdminNombre = model.UsuarioAdminName;
                u.UsuarioAdminPassword = model.UsuarioAdminPassword;
                bool boolean = _usuariosI.existUsuario(u);
                if (boolean)
                {
                    FormsAuthentication.SetAuthCookie(model.UsuarioAdminName, false /* createPersistentCookie */);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return View(model);
                }
            }
                  
            else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
                }
            return View(model);
        }
      

        public ActionResult LogOnF()
        {
            // User was redirected here because of authorization section
            if (User.Identity != null && User.Identity.IsAuthenticated)
                return RedirectToAction("LogOn", "Account");

            return View();
        }

        //
        // POST: /Account/LogOn

        [HttpPost]
        public ActionResult LogOnF(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                    FormsAuthentication.SetAuthCookie(model.UsuarioAdminName, model.RememberMe);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

        //
        // GET: /Account/LogOff

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home");
        }
        
        
        //GET: /Account/Register
       [HttpGet]
       public ActionResult Register()
        {
            return View();
        }

       


       // POST: /Account/Register
       [HttpPost]
       public ActionResult Register(RegisterModel model)
       {
           if (ModelState.IsValid)
           {
               // Attempt to register the user
               UsuarioAdminModel u = new UsuarioAdminModel();
               u.UsuarioAdminNombre = model.UsuarioAdminName;
               u.UsuarioAdminPassword = model.UsuarioAdminPassword;
               _usuariosI.addUsuario(u);
               FormsAuthentication.SetAuthCookie(model.UsuarioAdminName, false /* createPersistentCookie */);
           }  
           //return View(model);
           return RedirectToAction("LogOn", "Account");
       }
    }
}
